package com.baizhi.controller;

import cn.hutool.captcha.*;
import cn.hutool.http.HttpResponse;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
//    @LogAnnotation(value = "登录页面",type = "login")
    public String login(Admin admin, HttpSession session,String verifyCodeActual){
        UsernamePasswordToken token=new UsernamePasswordToken(admin.getUserName(),admin.getPassword());
//       //从session作用域获取验证码
//        ShearCaptcha code= (ShearCaptcha) session.getAttribute("getCode");
//        System.out.println(code.toString());
//        if(code.verify(verifyCodeActual)){
//            Admin admin1=adminService.login(admin);
//                if(admin1!=null){
//                    session.setAttribute("admin",admin1);
//                    return "redirect:/jsp/cmfz_houtai.jsp";
//                }else{
//                    return "redirect:/login.jsp";
//                }
//        }else{
//            return "redirect:/login.jsp";
//        }



        //2.获取subject
        Subject subject= SecurityUtils.getSubject();
        //从session作用域获取验证码
        ShearCaptcha code= (ShearCaptcha) session.getAttribute("getCode");
        System.out.println(code);
        if(code.verify(verifyCodeActual)){
            try{
                //3.调用subject登录方法
                subject.login(token);
                boolean permitted = subject.isPermitted("banner:create");
                if(permitted){
                    System.out.println(permitted);
                }
                return "redirect:/jsp/cmfz_houtai.jsp";
            }catch (Exception e){
                return "redirect:/login.jsp";
            }
        }else{
            return "redirect:/login.jsp";
        }


    }

    //验证码
    @RequestMapping("/getCode")
//    @LogAnnotation(value = "获取验证码",type = "getCode")
    public void getCode(HttpSession session,HttpServletResponse response) throws IOException {
        //调用糊涂工具
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
        //将创建的验证码放入到session作用域
        session.setAttribute("getCode",captcha);
        //输入流到浏览器
        captcha.write(response.getOutputStream());
        System.out.println(captcha);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Map insert(Admin admin){
        return adminService.insert(admin);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map delete(int id){
        return adminService.delete(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map update(Admin admin){
        return adminService.update(admin);
    }

    @RequestMapping("/selectById")
    @ResponseBody
    public Admin selectById(int id){
        return adminService.selectById(id);
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public Map selectAll(int page,int limit,String userName){
        return adminService.selectAll(page, limit, userName);

    }


}
